﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1;
using System.Data.SqlClient;

namespace DA_CNPM
{
    public partial class formSach_Thanhvien : Form
    {
        private long mathanhvien;
        Database database = new Database();
        Database database1 = new Database();
        SqlCommand cmd, cmd1;
        SqlDataReader rdr = null;
        SqlDataReader rdr1 = null;
        List<Control> component_Sach = new List<Control>();
        List<Control> component_Ngaymuon = new List<Control>();
        List<Control> component_Hantra = new List<Control>();
        public formSach_Thanhvien(long mathanhvien)
        {
            InitializeComponent();
            this.mathanhvien = mathanhvien;
            database.conn.Open();
            database1.conn.Open();
            setDynamicNameComponent();
            setValues();
        }

        public void setValues()
        {
            string selectString = @"select * from phieumuon where mathanhvien = " + mathanhvien;
            cmd = new SqlCommand(selectString, database.conn);
            rdr = cmd.ExecuteReader();
            int sosachdangmuon = 0;
            int i = 0 ; 
            while (rdr.Read())
            {
                sosachdangmuon++;
                component_Sach[i].Text = Convert.ToInt64(rdr[1]).ToString();
                component_Ngaymuon[i].Text = Convert.ToDateTime(rdr[4]).ToString("dd-MM-yyyy");
                component_Hantra[i].Text = Convert.ToDateTime(rdr[5]).ToString("dd-MM-yyyy");
                i++;
            }
            rdr.Close();
            database.conn.Close();
            //int j = 0;
            //while (j < i)
            //{
            //    selectString = @"select tensach from sach where masach = " + component_Sach[j].Text;
            //    cmd1 = new SqlCommand(selectString, database1.conn);
            //    rdr1 = cmd.ExecuteReader();
            //    while (rdr1.Read())
            //    {
            //        component_Sach[j].Text = Convert.ToString(rdr1[2]);
            //        j++;
            //    }
            //}
            tbSosachdangmuon.Text = sosachdangmuon.ToString();
        }

        public void setDynamicNameComponent()
        {
            component_Sach.Add(tbSach1);
            component_Sach.Add(tbSach2);
            component_Sach.Add(tbSach3);
            component_Sach.Add(tbSach4);
            component_Sach.Add(tbSach5);

            component_Ngaymuon.Add(tbDateMuonSach1);
            component_Ngaymuon.Add(tbDateMuonSach2);
            component_Ngaymuon.Add(tbDateMuonSach3);
            component_Ngaymuon.Add(tbDateMuonSach4);
            component_Ngaymuon.Add(tbDateMuonSach5);

            component_Hantra.Add(tbDateTraSach1);
            component_Hantra.Add(tbDateTraSach2);
            component_Hantra.Add(tbDateTraSach3);
            component_Hantra.Add(tbDateTraSach4);
            component_Hantra.Add(tbDateTraSach5);
        }
    }
}
